<template>
  <div>
    <div>
      <div style="text-align: right;">
        <el-button v-has="30201" size="small" type="primary" @click="add">新增</el-button>
      </div>
      <el-divider/>
      <el-table ref="multipleTable" :data="table_body" stripe style="width: 100%">
        <el-table-column prop="shortName" label="地址简称" />
        <el-table-column prop="company" label="公司名" />
        <el-table-column prop="contacter" label="姓名" />
        <el-table-column prop="phone" label="电话" />
        <el-table-column prop="countryCode" label="国家" />
        <el-table-column prop="state" label="州/省" />
        <el-table-column prop="city" label="城市" />
        <el-table-column prop="zipCode" label="邮编" />
        <el-table-column prop="address" label="地址1" />
        <el-table-column prop="address2" label="地址2" />
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button v-has="30202" size="small" shape="circle" type="text" @click="edit(scope.row)">编辑</el-button>
            <el-button v-has="30203" size="small" shape="circle" type="text" @click="del(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination background :current-page="queryData.currentPage" :page-sizes="[50,100]" :page-size="queryData.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="pageTotal" style="float:right;" @size-change="onPageSizeChange" @current-change="onPageChange" />

    </div>
    <div>
      <el-dialog :visible.sync="openModal" :title="drawerTitle" width="60%" top>
        <el-form :model="formData">
          <el-row :gutter="32">
            <el-col :span="12">
              <el-form-item label="地址简称" label-position="top" required>
                <el-input v-model="formData.shortName" placeholder="地址简称" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="公司名称" label-position="top">
                <el-input v-model="formData.company" placeholder="公司名称" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="32">
            <el-col :span="12">
              <el-form-item label="姓名" label-position="top" required>
                <el-input v-model="formData.contacter" placeholder="收件人姓名" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="电话" label-position="top" required>
                <el-input v-model="formData.phone" placeholder="收件人电话" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="32">
            <el-col :span="12">
              <el-form-item label="国家" label-position="top" required>
                <el-select v-model.lazy="formData.countryCode" clearable filterable style="width:100%">
                  <el-option v-for="item in countrys" :key="item.code" :value="item.code" :label="item.nameCn"/>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="省/州" label-position="top" required>
                <el-input v-model="formData.state" placeholder="收件人省/州" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="32">
            <el-col :span="12">
              <el-form-item label="城市" label-position="top" required>
                <el-input v-model="formData.city" placeholder="收件人城市" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="邮编" label-position="top" required>
                <el-input v-model="formData.zipCode" placeholder="目的地邮编" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="32">
            <el-col :span="24">
              <el-form-item label="地址1" label-position="top" required>
                <el-input v-model="formData.address" placeholder="地址1" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="32">
            <el-col :span="24">
              <el-form-item label="地址2" label-position="top">
                <el-input v-model="formData.address2" placeholder="地址2" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button style="margin-right: 8px" @click="openModal = false">取消</el-button>
          <el-button type="primary" @click="submit">确定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import request from '@/utils/request'
import { copyProperty } from '@/utils/tools'
export default {
  data() {
    return {
      openModal: false,
      table_body: [

      ],
      formDataDefault: {
        shortName: '',
        contacter: '',
        phone: '',
        countryCode: '',
        state: '',
        city: '',
        address: '',
        address2: '',
        company: '',
        zipCode: ''
      },
      formData: {
        id: '',
        shortName: '',
        contacter: '',
        phone: '',
        countryCode: '',
        state: '',
        city: '',
        address: '',
        address2: '',
        company: '',
        zipCode: ''
      },
      queryData: {
        pageSize: 50,
        currentPage: 0
      },
      pageTotal: 0,
      pageSizeOpts: [50, 100],
      drawerTitle: '新增'
    }
  },
  computed: {
    pageSize() {
      return this.pageSizeOpts[0]
    }
  },
  created() {
    this.list()
    this.countrys()
  },
  mounted() {
  },
  methods: {
    list() {
      request({
        url: 'basesetting/owner/deliver/query',
        data: this.queryData,
        method: 'post'
      }).then(res => {
        if (res.success) {
          this.loadedDate = true
          this.table_body = res.data.records
          this.pageTotal = res.data.total
        }
      })
    },
    checkData(data, schema) {
      for (const x in data) {
        if (!schema.hasOwnProperty(x)) {
          continue
        }
        if (data[x] instanceof Array && data[x].length === 0) {
          this.$message({
            showClose: true,
            message: schema[x].message,
            type: 'error',
            duration: 0
          })
          return false
        } else if (data[x] instanceof Object) {
          if (!this.checkData(data[x], schema[x])) {
            return false
          }
        } else {
          if (!data[x]) {
            this.$message({
              showClose: true,
              message: schema[x].message,
              type: 'error',
              duration: 0
            })
            return false
          }
        }
      }
      return true
    },
    submit() {
      const schema = {
        shortName: { message: '地址简称必填' },
        contacter: { message: '姓名必填' },
        phone: { message: '电话必填' },
        countryCode: { message: '国家必填' },
        state: { message: '省/州必填' },
        city: { message: '城市必填' },
        address: { message: '地址必填' },
        zipCode: { message: '邮编必填' }
      }
      if (!this.checkData(this.formData, schema)) {
        return
      }
      request({
        url: 'basesetting/owner/deliver',
        method: 'post',
        data: this.formData
      }).then(res => {
        if (res.success) {
          this.openModal = false
          this.list()
        }
      })
    },
    edit(row) {
      copyProperty(this.formData, row)
      this.drawerTitle = '编辑'
      this.openModal = true
    },
    del(row) {
      const _this = this
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
         _this.doDel(row.id)
      })
    },
    doDel(id) {
      request({
        url: 'basesetting/owner/deliver/del?id=' + id,
        method: 'get'
      }).then(res => {
        if (res.success) {
         this.list()
        }
      })
    },
    add() {
      copyProperty(this.formData, this.formDataDefault)
      this.packingListTableBody = []
      this.editable = true
      this.openModal = true
    },
    countrys() {
      request({
        url: '/countrys',
        method: 'get'
      }).then(res => {
        if (res.success) {
          if (res.data) {
            this.countrys = res.data
          }
        }
      })
    },
    onPageSizeChange(pageSize) {
      this.queryData.pageSize = pageSize
      this.list()
    },
    onPageChange(page) {
      this.queryData.currentPage = page
      this.list()
    }

  }
}
</script>
<style>
</style>
